package com.bcgtgjyb.huanwen.customview.mylibrary;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by guohuanwen on 2015/10/6.
 */
public class FourCirlceRotate extends Component implements Component.DrawTask {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private Paint paint1;
    private Paint paint2;
    private Paint paint3;
    private Paint paint4;
    private Paint paint5;
    private DataAnimatorValue valueAnimator;
    private boolean isInit = false;
    private float mR = 0;
    private boolean isStop = false;

    private float where = 0;
    private boolean isStart = false;
    private int show = 0;

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public FourCirlceRotate(Context context, AttrSet attrs) {
        super(context, attrs);
        addDrawTask(this::onDraw);
        paint1 = new Paint();
        paint2 = new Paint();
        paint3 = new Paint();
        paint4 = new Paint();
        paint5 = new Paint();
        paint1.setColor(new Color(Color.getIntColor("#ff0099cc")));
        paint2.setColor(new Color(Color.getIntColor("#ff669900")));
        paint3.setColor(new Color(Color.getIntColor("#ffcc0000")));
        paint4.setColor(new Color(Color.getIntColor("#ffaa66cc")));
        paint5.setColor(new Color(Color.getIntColor("#ffffbb33")));
    }

    /**
     * 动画开始
     */
    public void startAnimation() {
        if (valueAnimator == null) {
            valueAnimator = getValueAnimator();
        } else {
            valueAnimator.start();
        }
        mR = (float) (getWidth() / 6d);
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                if (!isStop) {
                    startAnimation();
                    invalidate();
                }
            }
        },10);

        isInit = true;
    }

    /**
     * 结束
     */
    public void stop() {
        this.isStop = true;
    }

    private DataAnimatorValue getValueAnimator() {
        DataAnimatorValue animator = new DataAnimatorValue();
        animator.setStart(0f);
        animator.setEnd(8f);
        animator.setDuration(4000);
        animator.setCurveType(Animator.CurveType.LINEAR);
        animator.start();
        return animator;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (!isInit) {
            startAnimation();
        }
        // 显示固定的圆，逐个出现
        if (show <= 4) {
            canvas.drawCircle(mR, mR, mR, paint2);
        }
        if (show <= 3) {
            canvas.drawCircle((float) ((double)getWidth() - mR), mR, mR, paint3);
        }
        if (show <= 2) {
            canvas.drawCircle((float) ((double)getWidth() - mR), (float) ((double)getHeight() - mR), mR, paint4);
        }
        if (show <= 1) {
            canvas.drawCircle(mR, (float) ((double)getHeight() - mR), mR, paint5);
        }
        // 逐个消失
        if (show > 4) {
            if (show <= 4) {
                canvas.drawCircle(mR, mR, mR, paint2);
            }
            if (show <= 5) {
                canvas.drawCircle((float) ((double)getWidth() - mR), mR, mR, paint3);
            }
            if (show <= 6) {
                canvas.drawCircle((float) ((double)getWidth() - mR), (float) ((double)getHeight() - mR), mR, paint4);
            }
            if (show <= 7) {
                canvas.drawCircle(mR, (float) ((double)getHeight() - mR), mR, paint5);
            }
        }
        // 移动
        if (where < 1 && where > 0) {
            canvas.drawCircle(mR + (getWidth() - 2 * mR) * where, mR, mR, paint1);
            show = 4;
        }
        if (where < 2 && where > 1) {
            canvas.drawCircle(getHeight() - mR, mR + (getHeight() - 2 * mR) * (where - 1), mR, paint1);
            show = 3;
        }
        if (where < 3 && where > 2) {
            canvas.drawCircle(getWidth() - mR - (getWidth() - 2 * mR) * (where - 2), getHeight() - mR, mR, paint1);
            show = 2;
        }
        if (where < 4 && where > 3) {
            canvas.drawCircle(mR, getHeight() - mR - (getHeight() - 2 * mR) * (where - 3), mR, paint1);
            show = 1;
        }
        if (where > 4 && where < 5) {
            canvas.drawCircle(mR + (getWidth() - 2 * mR) * (where - 4), mR, mR, paint1);
            show = 5;
        }
        if (where > 5 && where < 6) {
            canvas.drawCircle(getHeight() - mR, mR + (getHeight() - 2 * mR) * (where - 5), mR, paint1);
            show = 6;
        }
        if (where > 6 && where < 7) {
            canvas.drawCircle(getWidth() - mR - (getWidth() - 2 * mR) * (where - 6), getHeight() - mR, mR, paint1);
            show = 7;
        }
        if (where > 7 && where < 8) {
            canvas.drawCircle(mR, getHeight() - mR - (getHeight() - 2 * mR) * (where - 7), mR, paint1);
            show = 8;
        }

        if (isStart) {
            where = (float) valueAnimator.getAnimatedValue();
        }
        if (valueAnimator.isRunning()) {
            isStart = true;
            invalidate();
        }
    }
}
